﻿using System;
using System.Threading;

namespace MP_Network
{
    public static class ServerTime
    {
        private static long _offsetMs; // 服务器-本地 毫秒差值
        public static void Sync(long offsetMilliseconds)
            => Interlocked.Exchange(ref _offsetMs, offsetMilliseconds);

        // 获取当前服务器时间
        public static long NowMs
            => DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + Interlocked.Read(ref _offsetMs);

        // 获取 DateTime
        public static DateTime UtcNow
            => DateTimeOffset.UtcNow.AddMilliseconds(Interlocked.Read(ref _offsetMs)).UtcDateTime;
    }
}
